import { window, ExtensionContext, commands, QuickPickItem, QuickPickOptions, workspace } from 'vscode';
import translateUtil  from './utils/translate';
import varCaseTypes from './utils/transform';
import { filterCharacter, stringTrim } from './utils/index';
// 翻译引擎
enum TranslateUtilType {
	deepl = "deepl",
	bing = "bing",
	google = "google",
  baidu = "baidu",
  tenCent = "tenCent",
  ali = "ali",
  youDao = "youDao",
}

enum ChineseAbbreviationType {
	google = "zh-CN",
	deepl = "ZH",
	bing = "zh-Hans",
  baidu = "zh",
  tenCent = "zh",
  ali = "zh",
  youDao = "zh-CHS",
}
enum EnglishAbbreviationType {
	google = "en",
	deepl = "EN",
	bing = "en",
  baidu = "en",
  tenCent = "en",
  ali = "en",
  youDao = "en",
}


interface CommandMapItemType {
	command: string,
	callback: () => {},
}

/**
 * 获取待注册命令集合
 * @returns { CommandMapItemType[] }
 */
const getCommandMap: () => CommandMapItemType[] = (): CommandMapItemType[] => {
	const commandList: CommandMapItemType[] = varCaseTypes().map(({ namingFormat }): CommandMapItemType => ({ command: `extension.var-translate-en.${namingFormat}`, callback: () => namingFormatSelected(namingFormat) }));
	return [
		{ command: 'extension.var-translate-en', callback: () => translateHandler('en') },
		{ command: 'extension.var-translate-zh', callback: () => translateHandler('zh') },
		...commandList
	];
};

/**
 * 翻译处理方法
 * @param { string } translateTo 要翻译成什么语言 默认英语(en)
 * 
 */
const translateHandler = async (translateTo: string = 'en') => {
	const { activeTextEditor } = window;
	// 获取用户选择翻译服务
	const	translateType: TranslateUtilType	= workspace.getConfiguration("var-translate-en").translateServe;
	const targetLanguage = translateTo === 'en' ? EnglishAbbreviationType[translateType] : ChineseAbbreviationType[translateType];

	if (!activeTextEditor) {
		return;
	}
	const originText = activeTextEditor.document.getText(activeTextEditor.selection);
	const translateResult = await asyncLanguageTranslate(translateTo === 'en' ?  originText : filterCharacter(originText), targetLanguage);
	if (!translateResult) {
		return;
	}
	let selected: string | undefined;
	if (translateTo === 'en') {
		selected = await namingFormatSelected(translateResult);
	} else {
		const quickPickItem = await window.showQuickPick([{ label: translateResult }], { title: '翻译结果如下（Translation Results Are As Follows）' });
	  quickPickItem &&	(selected = quickPickItem.label);
	}
	// 用户选择命令类型
	if (!selected) {
		return;
	}
	// 替换文本
	activeTextEditor.edit((builder) => builder.replace(activeTextEditor.selection, selected as string));
};

/**
 * 翻译引擎 网络翻译
 * @param { string } originText 待翻译的内容
 * @param { string } translateTo 要翻译成什么语言 默认英语(en)
 * @returns { string | undefined  }
 */
const asyncLanguageTranslate = async (originText: string, translateTo: string): Promise<string | undefined> => {
	// 获取用户选择翻译服务
  const	translateType: TranslateUtilType	= workspace.getConfiguration("var-translate-en").translateServe;
	originText = stringTrim(originText);
	if (!originText) {
		return;
	}
	// 英文直接返回
	if (/^[a-zA-Z_-]$/u.test(originText)) {
		return originText;
	}
	const foreign = [
		{ name: 'deepl', alias: 'DeepL', undulate: true}, 
	{ name: 'google', alias: '谷歌', undulate: true}, 
	{ name: 'bing', alias: '必应', undulate: true},
	{ name: 'baidu', alias: '百度', undulate: false},
	{ name: 'tenCent', alias: '腾讯', undulate: false},
	{ name: 'ali', alias: '阿里', undulate: false},
	{ name: 'youDao', alias: '有道', undulate: false},
];
	const findForeign = foreign.find(item => item.name === translateType);
	window.showQuickPick([{ label: `🚀当前使用${findForeign?.alias}翻译中 请稍等...${findForeign?.undulate ? '(' + findForeign.alias + '翻译会受网络环境影响、建议配置国内翻译厂商)' : ''}` }]);
	try {
		const translateResult = await translateUtil[translateType].translate(originText, translateTo);
		return translateResult;
	} catch (err) {
		window.showQuickPick([{ label: '翻译出错，详细错误信息请看右下角错误提示弹窗' }]);
		window.showErrorMessage(`翻译失败，请检查网络链接或配置项是否正确
		错误信息: ${JSON.stringify(err)}`);
		return;
	}
};

/**
 * 命名风格选择
 * @param { string} transformContent 待转换的文本内容
 * @returns { string | undefined  }
 */
const namingFormatSelected = async (transformContent: string): Promise<string | undefined> => {
	const selectList: QuickPickItem[] = varCaseTypes().map((item): QuickPickItem => ({
    label: item.transformHandler(transformContent),
    description: item.description,
  }));
  const selectDescription: QuickPickOptions = { matchOnDescription: true, title: '翻译结果如下（Translation Results Are As Follows）', placeHolder: "选择想要的命名风格，点击或回车替换 (Choose to replace)" };
  const selected: QuickPickItem | undefined = await window.showQuickPick(selectList, selectDescription);
  if (!selected) {
    return;
  }
  return selected.label;
};

export function activate(context: ExtensionContext): void {
	const pendingRegistrationCommandList: CommandMapItemType[] = getCommandMap();
	// 注册指令
	pendingRegistrationCommandList.forEach(({ command, callback }): void => {
    context.subscriptions.push(commands.registerCommand(command, callback));
  });
}


export function deactivate() {}
